package server

import constants.JobConstants
import provider.MapleDataTool
import server.img.MapleWZImg
import java.awt.Point
import java.util.*


/**
 * 免责声明：本模拟器源代码下载自ragezone.com，仅用于技术研究学习，无任何商业行为。
 */
object MapleUnionFactory {

    private val boardInfos = HashMap<Int, BoardInfoEntry>()
    private val characterSizes = HashMap<Int, HashMap<Int, HashMap<Int, Point>>>()
    private val boardInfoSkillIDs = HashMap<Int, Int>()
    private val cardSkillIDs = HashMap<Int, Int>()
    private val unionRanks = HashMap<Int, HashMap<Int, UnionRankEntry>>()

    fun getUnionRank(): Map<Int, HashMap<Int, UnionRankEntry>> {
        return unionRanks
    }

    fun getCharacterSizeData(n2: Int, n3: Int): Map<Int, Point>? {
        if (characterSizes.containsKey(n2) && (characterSizes[n2] as Map<*, *>).containsKey(n3)) {
            return characterSizes[n2]!![n3]
        }
        return null
    }

    fun getBoardInfos(): Map<Int, BoardInfoEntry> {
        return boardInfos
    }

    fun getCardSkillIDs(job: Int, level: Int): Triple<Int, Int, Int>? {
        val skillid = this.cardSkillIDs[job / 10] as Int
        if (skillid <= 0) {
            return null
        }
        return Triple(skillid - 71000000, skillid, getGrade(job, level))
    }

    fun getGrade(job: Int, level: Int): Int {
        if (JobConstants.is神之子(job)) {
            if (level in 130..159) {
                return 1
            }
            if (level in 160..179) {
                return 2
            }
            if (level in 180..199) {
                return 3
            }
            if (level in 200..249) {
                return 4
            }
            if (level >= 250) {
                return 5
            }
        } else {
            if (level in 60..99) {
                return 1
            }
            if (level in 100..139) {
                return 2
            }
            if (level in 140..199) {
                return 3
            }
            if (level in 200..249) {
                return 4
            }
            if (level >= 250) {
                return 5
            }
        }
        return -1
    }

    fun init() {
        val mapleUnionData = MapleWZImg.etc.getData("mapleUnion.img")
        for (boardinfo in mapleUnionData.getChildByPath("BoardInfo")) {
            boardInfos[Integer.parseInt(boardinfo.name)] = BoardInfoEntry(MapleDataTool.getInt("xPos", boardinfo, 0), MapleDataTool.getInt("yPos", boardinfo, 0), MapleDataTool.getInt("changeable", boardinfo, 0) > 0, MapleDataTool.getInt("groupIndex", boardinfo, 0), MapleDataTool.getInt("openLevel", boardinfo, 0))
        }
        for (characterSizeData in mapleUnionData.getChildByPath("CharacterSize")) {
            val id = Integer.parseInt(characterSizeData.name)
            var characterSize = characterSizes[id]
            if (characterSize == null) {
                characterSize = HashMap()
                characterSizes[id] = characterSize
            }
            for (subData in characterSizeData) {
                val int2 = Integer.parseInt(subData.name)
                var points: HashMap<Int, Point>? = characterSize[int2]
                if (points == null) {
                    points = HashMap()
                    characterSize[int2] = points
                }
                for (subData_ in subData) {
                    points[Integer.parseInt(subData_.name)] = MapleDataTool.getPoint(subData_)
                }
            }
        }
        for (b5 in mapleUnionData.getChildByPath("BoardInfo")) {
            boardInfoSkillIDs[Integer.parseInt(b5.name)] = MapleDataTool.getInt("skillID", b5, 0)
        }
        for (b6 in mapleUnionData.getChildByPath("Card")) {
            cardSkillIDs[Integer.parseInt(b6.name)] = MapleDataTool.getInt("skillID", b6, 0)
        }
        for (unionRankData in mapleUnionData.getChildByPath("unionRank")) {
            val name = MapleDataTool.getString(unionRankData.getChildByPath("info/name"))
            val unionRankEntrys = HashMap<Int, UnionRankEntry>()
            val rank = Integer.parseInt(unionRankData.name)
            for (subData in unionRankData) {
                if (subData.name != "info") {
                    val subRank = Integer.parseInt(subData.name)
                    unionRankEntrys[subRank] = UnionRankEntry(name, rank, subRank, MapleDataTool.getInt("level", subData, 0), MapleDataTool.getInt("attackerCount", subData, 0), MapleDataTool.getInt("coinStackMax", subData, 0))
                }
            }
            unionRanks[rank] = unionRankEntrys
        }
    }

}

data class UnionRankEntry(private val name: String, val rank: Int, val subRank: Int, val level: Int, val attackerCount: Int, val coinStackMax: Int) {

    override fun toString(): String {
        return this.name + "第" + this.subRank + "阶段"
    }
}


data class BoardInfoEntry(val xPos: Int, val yPos: Int, val changeable: Boolean, val groupIndex: Int, val openLevel: Int)